<?php


	namespace app\admin\model\scheduling;


	use core\basic\Db;
	use core\basic\Model;

	class SchedulingModel extends Model
	{


		// 检查订单信息
		public function checkScheduling()
		{
			return parent::table('ay_scheduling')->where(['id' => 1])->find();
		}

		// 修改订单信息
		public function modScheduling($data)
		{
			return parent::table('ay_scheduling')->where(['id' => 1])->update($data);
		}

		// 增加订单信息
		public function addScheduling($data)
		{
			return parent::table('ay_scheduling')->insert($data);
		}

		// 获取订单信息
		public function getList()
		{
			return parent::table('ay_scheduling')->where(['id' => 1])->find();
		}

		//获取订单信息
		public function getScheduling($id)
		{
			return parent::table('ay_scheduling')->where(['id' => $id])->find();
		}

		//获取今天已经抢到排单的用户比例
		public function getProportion($where)
		{
			$field = array(
				's.*',
				'u.usermobile'
			);
			$join = array(
				'ay_member u',
				's.user_id=u.id',
				'LEFT'
			);

			return parent::table('ay_schelist s')->field($field)
				->join($join)
				->order('s.id desc')
				->where($where)
				->page()
				->select();

		}

		//添加用户抢到的排单
		public function addProportion($data)
		{
			return parent::table('ay_schelist')->insert($data);
		}

		//获取排单
		public function GetUserSchedu($id = '1')
		{
			return parent::table('ay_scheduling')->where(['id' => $id])->find();
		}

		//添加排单利润
		public function Addsingle_profit($data)
		{
			return parent::table('ay_single_profit')->insert($data);
		}

		//排单利润列表
		public function profitList($where = '')
		{
			return parent::table('ay_single_profit')->where($where)->select();
		}

		// 修改排单利润
		public function modProfit($id, $data)
		{

			return parent::table('ay_single_profit')->where("id=$id")->update($data);
		}

		// 获取排单利润
		public function getProfit($id)
		{
			return parent::table('ay_single_profit')
				->where("id=$id")
				->find();

		}

		//添加订单池
		public function SchedulingAdd($data)
		{
			return parent::table('ay_pool')->insert($data);
		}

//订单池列表
		public function PoolList()
		{
			return parent::table('ay_pool')->page(1, 10)->select();
		}

		public function NewPool($where)
		{
			return parent::table('ay_pool')->where($where)->select();
		}

		//删除订单池
		public function delPool($id)
		{
			return parent::table('ay_pool')->where("id=$id")->delete();
		}

		//查询当前排单池
		public function PoolInfo($where)
		{
			return parent::table('ay_pool')->where($where)->find();
		}

		//修改当前排单池
		public function ModPool($id, $data)
		{
			return parent::table('ay_pool')->where("id=$id")->update($data);
		}

		//添加当前排单池
		public function addUserPool($data)
		{
			return parent::table('ay_user_pool')->insert($data);
		}

		//查询会员的订单池
		public function UserPoolInof($where)
		{
			return parent::table('ay_user_pool')->where($where)->select();
		}

		//所有用户抢到的订单池
		public function UserPoolList()
		{
			$field = array(
				'p.*',
				'u.usermobile',
				'd.pname',
			);
			$join = array(
				'ay_member u',
				'p.user_id=u.id',
				'LEFT'
			);
			$joins = array(
				'ay_pool d',
				'p.pool_id=d.id',
				'LEFT'
			);
			return parent::table('ay_user_pool p')->field($field)
				->join($join)
				->join($joins)
				->order('p.add_time desc')
				->page()
				->select();

		}
		//搜索用户抢到的订单池
		public function findUserPoolList($fields,$keyword)
		{
			$field = array(
				'p.*',
				'u.usermobile',
				'd.pname',
			);
			$join = array(
				'ay_member u',
				'p.user_id=u.id',
				'LEFT'
			);
			$joins = array(
				'ay_pool d',
				'p.pool_id=d.id',
				'LEFT'
			);
			return parent::table('ay_user_pool p')->field($field)
				->join($join)
				->join($joins)
				->order('p.add_time desc')
				->like($fields, $keyword)
				->page()
				->select();

		}
	}